package com.collectplus.express.self;

import android.content.Intent;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.tools.DataUtils;
import com.collectplus.express.tools.TextWatcherExt;

import java.util.HashMap;
import java.util.Map;

import droid.frame.activity.title.TitleRes;
import droid.frame.utils.lang.Str;
import droid.frame.view.InputFilterExt;

/**
 * 添加备注界面
 *
 * @author HuXinDan<br>
 *         2016-11-3上午10:15:47
 */
public class SelfRemarkActivity extends BaseActivity2 {

    private EditText mRemark;
    private SelfRemarkAdapter mAdapter = null;
    TextView mHasNumTV;// 用来显示剩余字数
    int num = 30;//限制的最大字数
    int number = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.self_add_remark);
        super.findViewById();
        getAppTitle().setCommonTitle("填写备注", new TitleRes("完成", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String remark = mRemark.getText().toString();
                Intent data = new Intent();
                data.putExtra("remark", remark);
                setResult(RESULT_OK, data);
                mAdapter.saveSelected();
                finish();
            }
        }));
        mRemark = (EditText) findViewById(R.id.self_add_remark);
        mHasNumTV = (TextView) findViewById(R.id.has_num_text_tv);
        GridView mGridView = (GridView) findViewById(R.id.grid_view);
        mAdapter = new SelfRemarkAdapter(DataUtils.getRemarkData(), getContext());
        mGridView.setAdapter(mAdapter);
        // title右侧 -完成按钮
        final TextView rightTitle = (TextView) findViewById(R.id.title_right_text);
        /**
         * 大概思路：根据输入的内容和标签的内容进行匹配，如果有两个相同的内容标签，那么当删除最后一个标签内容的时候，再进行标签状态改变
         * 1.遍历标签的内容，然后和输入的文本框进行字符串对比，如果有相同的内容，则进行选中状态
         */
        mRemark.addTextChangedListener(new TextWatcherExt() {
            private int selectionEnd;

            @Override
            public void afterTextChanged(Editable s) {
                rightTitle.setTextColor(getResources().getColor(R.color.app_font_1));
                number = s.length();
                mHasNumTV.setText(number + "/" + num);
                selectionEnd = mRemark.getSelectionEnd();
                if (s.length() > num) {
                    s.delete(num, selectionEnd);
                    int tempSelection = selectionEnd;
                    mRemark.setText(s);
                    mRemark.setSelection(tempSelection);//设置光标在最后
                }
                //如果没有选择标签，而是自己输入的，则不用进行处理
                if (mAdapter.getSelected().size() == 0) {

                }
                //选择了标签，并且自己也进行了输入，那么需要判断
                else {
                    HashMap<String, Integer> map = containsLabel();
                    if (map.size() > 0) {
                        for (int i = 0; i < DataUtils.getRemarkData().size(); i++) {
                            String content = DataUtils.getRemarkData().get(i);
                            //如果map的key包含了标签，说明文本框中还有和标签内容一样的，则不进行移除
                            if (map.containsKey(content) && map.get(content) > 0) {

                            } else {
                                //注意，当前的i和adapter中的position相对应
                                mAdapter.removeSelected(i);
                            }
                        }
                    } else {
                        mAdapter.removeSelected();
                    }
                }
            }
        });
        mRemark.setFilters(new InputFilter[]{new InputFilterExt(4), new InputFilterExt(5)});
        //
        String remark = getIntent().getStringExtra("remark");
        mRemark.setText(Str.filterEditText(remark));
        /**
         * 如果要输入的标签大于剩余可输入的字符个数，那么就不让进行点击
         */
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //遍历hashset，看看是否之前已经选中过，如果没有选中过再进行选择，否则进行取消操作
                String item = (String) parent.getAdapter().getItem(position);
                if (mAdapter.getSelected().size() == 0) {
                    if (num - number < item.length() + 1) {
                        return;
                    }
                    mAdapter.setSelected(position);
                    mRemark.append(item + ";");
                } else {
                    //如果里面包含有之前选中的位置
                    //之前选中过，则进行移除操作
                    if (mAdapter.getSelected().contains(position) && mRemark.getText().toString().contains(item)) {
                        mAdapter.removeSelected(position);
                        mRemark.setText(mRemark.getText().toString().replace(item + ";", ""));
                    } else {
                        if (num - number < item.length() + 1) {
                            return;
                        }
                        mAdapter.setSelected(position);
                        mRemark.append(item + ";");
                    }
                }
            }
        });
    }

    /**
     * 遍历输入框的文字是否包含标签
     */
    private HashMap<String, Integer> containsLabel() {
        HashMap<String, Integer> hashMap = new HashMap<>();
        String remarkContent = mRemark.getText().toString();
        for (int i = 0; i < DataUtils.getRemarkData().size(); i++) {
            String contant = DataUtils.getRemarkData().get(i);
            if (remarkContent.contains(contant)) {
                int index = 0;
                for (int j = 0; j < remarkContent.length() - contant.length() + 1; j++) {
                    String str = remarkContent.substring(j, j + contant.length());
                    if (contant.equals(str)) {
                        index++;
                        hashMap.put(contant, index);
                    }
                }
            }
        }
        return hashMap;
    }

}
